package cn.yuan.xiaoyu.mainmodule.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.List;

import cn.yuan.xiaoyu.R;
import cn.yuan.xiaoyu.commonmodule.utils.ListUtils;
import cn.yuan.xiaoyu.mainmodule.activity.MainActivity;
import cn.yuan.xiaoyu.mainmodule.bean.MainListBean;

/**
 * Created by yukuoyuan on 2017/2/27.
 * 这是一个主界面列表的适配器
 */

public class MainListAdapter extends RecyclerView.Adapter<MainListAdapter.MyHolder> {
    List<MainListBean> mainData;
    MainActivity mainActivity;
    private OnItemClicklistener onItemClickListener;

    public MainListAdapter(List<MainListBean> mainData, MainActivity mainActivity) {
        this.mainData = mainData;
        this.mainActivity = mainActivity;
    }

    public interface OnItemClicklistener {
        void onItemClick(int type);
    }

    public void setOnItemClickListener(OnItemClicklistener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(parent.getContext(), R.layout.item_main, null);
        return new MyHolder(view);
    }

    @Override
    public void onBindViewHolder(final MyHolder holder, final int position) {
        holder.tvItemMainName.setText(mainData.get(position).name);
        Glide.with(mainActivity).load(mainData.get(position).img).into(holder.ivItemMainImg);
        holder.ivItemMainImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener!=null){
                    onItemClickListener.onItemClick(mainData.get(position).type);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        if (ListUtils.isEmpty(mainData)) {
            return 0;
        }
        return mainData.size();
    }

    class MyHolder extends RecyclerView.ViewHolder {

        private final ImageView ivItemMainImg;
        private final TextView tvItemMainName;

        public MyHolder(View itemView) {
            super(itemView);
            ivItemMainImg = (ImageView) itemView.findViewById(R.id.iv_item_main_img);
            tvItemMainName = (TextView) itemView.findViewById(R.id.tv_item_main_name);
        }
    }
}
